//登录注册模块
import {reqGetCode, reqUserLogin, reqUserRegister} from "@/api";

const state = {
    code: '',
    token:''
}
const mutations = {
    GET_CODE(state, code) {
        state.code = code
    },
    USER_LOGIN(state, token) {
        state.token = token
    }
}
const actions = {
    //获取验证码
    async getCode({commit}, phone) {
        let result = await reqGetCode(phone)
        if (result.code === 200) {
            commit('GET_CODE', result.data);
            return 'ok';
        } else {
            return Promise.reject(new Error('failed'))
        }
    },
    //用户注册
    async userRegister({commit}, user) {
        let result = await reqUserRegister(user)
        if (result.code === 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error(result.message))
        }
    },
    //用户登录
    async userLogin({commit}, user) {
        let result = await reqUserLogin(user)
        if (result.code === 200) {
            commit('USER_LOGIN', result.data.token);
            return 'ok';
        } else {
            return Promise.reject(new Error(result.message))
        }
    }
}
const getters = {}
export default {
    state,
    mutations,
    actions,
    getters
}